package com.tb.shoop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tb.v1.api.shoop.ISpAttributeService;
import com.tb.v1.common.exception.CommonException;
import com.tb.v1.entity.SpAttribute;
import com.tb.v1.result.ResultJson;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: tangBin
 * @Date: 2020/7/21 0021 15:45
 * @Description:
 */
@RequestMapping("/v1")
@RestController
public class SpAttributeController extends BaseController {
    @Reference
    private ISpAttributeService spAttributeService;

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 商品管理 分类参数列表
     * @Date 2020/7/21 0021 15:51
     * @Param [catId, attrSel]
     **/
    @RequestMapping("attributeList/{catId}")
    public ResultJson<Object> getAttributeList(@PathVariable("catId") String catId, String attrSel) throws Exception {
        if (StringUtils.isEmpty(catId) || StringUtils.isEmpty(attrSel)) {
            throw new CommonException("500", "参数为空");
        }
        Map map = new HashMap();
        map.put("catId", catId);
        map.put("attrSel", attrSel);
        List<SpAttribute> list = spAttributeService.getAttributeList(map);
        return toResultModel("200", "成功", list);
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 添加分类参数
     * @Date 2020/7/21 0021 16:36
     * @Param [catId, attrSel]
     **/
    @RequestMapping("add/attribute")
    public ResultJson<Object> getAddAttribute(@RequestParam("catId") String catId,
                                              @RequestParam("attrName") String attrName,
                                              @RequestParam("attrSel") String attrSel
    ) throws Exception {
        if (StringUtils.isEmpty(catId)
                || StringUtils.isEmpty(attrSel)
                || StringUtils.isEmpty(attrName)) {
            throw new CommonException("500", "参数为空");
        }
        SpAttribute spAttribute = new SpAttribute();
        spAttribute.setAttrName(attrName);
        spAttribute.setAttrSel(attrSel);
        //spAttribute.setAttrVals(attrVals);
        spAttribute.setCatId(Integer.valueOf(catId));
        spAttribute.setAttrWrite("list");
        Integer num = spAttributeService.saveEntity(spAttribute);
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("500", "失败");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 商品分类 修改分类信息
     * @Date 2020/7/21 0021 17:28
     * @Param [attrId, attrName]
     **/
    @RequestMapping("update/attribute")
    public ResultJson<Object> getUpdateAttribute(@RequestParam("attrId") String attrId,
                                                 @RequestParam("attrName") String attrName) throws Exception {
        if (StringUtils.isEmpty(attrId)
                || StringUtils.isEmpty(attrName)) {
            throw new CommonException("500", "参数为空");
        }
        SpAttribute spAttribute = new SpAttribute();
        spAttribute.setAttrName(attrName);
        spAttribute.setAttrId(Integer.valueOf(attrId));
        Integer num = spAttributeService.updateEntity(spAttribute);
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("500", "失败");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 分类参数 删除分类
     * @Date 2020/7/21 0021 17:36
     * @Param [attrId, attrName]
     **/
    @RequestMapping("delete/attribute")
    public ResultJson<Object> getDeleteAttribute(@RequestParam("attrId") String attrId) throws Exception {
        if (StringUtils.isEmpty(attrId)) {
            throw new CommonException("500", "参数为空");
        }
        Integer num = spAttributeService.deleteEntity(Long.valueOf(attrId));
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("500", "失败");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 修改分类参数 可选值列表
     * @Date 2020/7/22 0022 11:08
     * @Param [attrId]
     **/
    @RequestMapping("update/attrVals")
    public ResultJson<Object> getUpdateAttrVals(@RequestParam("attrId") String attrId,
                                                @RequestParam("attrVals") String attrVals) throws Exception {
        if (StringUtils.isEmpty(attrId)) {
            throw new CommonException("500", "参数为空");
        }
        SpAttribute spAttribute = new SpAttribute();
        spAttribute.setAttrId(Integer.valueOf(attrId));
        spAttribute.setAttrVals(attrVals);
        Integer num = spAttributeService.updateEntity(spAttribute);
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("500", "失败");
        }
    }
}
